package middleware

import (
	"gitee.com/freedom_feel/gui-bos-api/common"
	"gitee.com/freedom_feel/gui-bos-api/global"
	"github.com/gin-gonic/gin"
	"net/http"
)

// AuthMiddleware 是一个身份验证中间件
func AuthMiddleware() gin.HandlerFunc {
	return func(g *gin.Context) {
		//reqCookie := g.Request.Header.Get("cookie")
		if uid, ok := common.VerifyCookie(g.Request.Cookies()); ok {
			// 如果认证通过，继续处理请求
			g.Next()
			common.GetUserIp(g)
			global.Uid = uid
			return
		}
		//认证不通过 返回
		g.Redirect(http.StatusFound, "/login_page")
		g.Abort()
		return
	}
}
